﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace Microsoft.IT.Crm.Utility.CrmSolutionManager
{
    public enum SolutionImportFlag
    {
        Import,
        Export
    }

    public enum OutputManagedFlag
    {
        Managed,
        Unmanaged
    }

    [Serializable]
    public class RunAsAccount
    {
        [XmlElement]
        public string Domain { get; set; }
        [XmlElement]
        public string Username { get; set; }
        [XmlElement]
        public string Password { get; set; }
    }

    [Serializable]
    public class CRMSolutionManagerConfig
    {
        private static CRMSolutionManagerConfig _instance = null;

        public static CRMSolutionManagerConfig Instance
        {
            get { return _instance; }
        }

        public static void Initialize(string xmlConfigFile)
        {
            if (!File.Exists(xmlConfigFile))
                throw new Exception("Configuration file error");

            try
            {
                var file = File.OpenRead(xmlConfigFile);
                XmlSerializer serializer = new XmlSerializer(typeof(CRMSolutionManagerConfig));
                _instance = (CRMSolutionManagerConfig)serializer.Deserialize(file);
                file.Close();
            }
            catch (Exception e)
            {
                throw new Exception("Configuration load failed!", e);
            }
        }

        [XmlElement]
        public string ServerName { get; set; }
        [XmlElement]
        public string ServerPort { get; set; }
        [XmlElement]
        public bool UsingSSL { get; set; }
        [XmlElement]
        public string OrganizationName { get; set; }
        [XmlElement]
        public SolutionImportFlag SolutionImportFlag { get; set; }
        [XmlElement]
        public string SolutionFilter { get; set; }
        [XmlElement]
        public string SolutionFolder { get; set; }
        [XmlElement]
        public OutputManagedFlag OutputManagedFlag { get; set; }
        [XmlElement]
        public string DefaultPublisher { get; set; }

        public RunAsAccount RunAsAccount { get; set; }
    }
}
